package com.zimmur.app.mocolink.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zimmur.app.mocolink.dao.AdminRoleMapper;
import com.zimmur.app.mocolink.dao.MenuMapper;
import com.zimmur.app.mocolink.dao.RoleMapper;
import com.zimmur.app.mocolink.dao.RoleMenuMapper;
import com.zimmur.app.mocolink.dao.RolePermissionMapper;
import com.zimmur.app.mocolink.domain.model.AdminRoleExample;
import com.zimmur.app.mocolink.domain.model.Role;
import com.zimmur.app.mocolink.domain.model.RoleExample;
import com.zimmur.app.mocolink.domain.util.StringUtils;
import com.zimmur.app.mocolink.domain.vo.MenuVo;
import com.zimmur.app.mocolink.domain.vo.RoleInfo;
import com.zimmur.app.mocolink.domain.vo.RoleVo;
import com.zimmur.app.mocolink.service.IRoleService;

@Service
public class RoleServiceImpl implements IRoleService {

	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private MenuMapper MenuMapper;
	@Autowired
	private RoleMenuMapper roleMenuMapper;
	@Autowired
	private RolePermissionMapper rolePermissionMapper;
	@Autowired
	private AdminRoleMapper adminRoleMapper;
	@Override
	public Set<String> selectRoleByAdminId(int adminId) {
		Set<String> roleSet=new HashSet<String>();
		List<Integer> roleIds = adminRoleMapper.selectByAdminId(adminId);
		if(roleIds!=null && roleIds.size()>0){
			RoleExample example = new RoleExample();
			example.createCriteria().andIdIn(roleIds);
			List<Role> roles = roleMapper.selectByExample(example);
			if(roles!=null && roles.size()>0){
				for(Role role:roles){
					roleSet.add(role.getName());
				}
			}
		}
		return roleSet;
	}

	@Override
	public List<RoleInfo> selectAllRoleWithAdminId(int adminId) {
		return roleMapper.selectRoleWithAdminId(adminId);
	}

	@Override
	public Role selectById(Integer roleId) {
		RoleVo roleVo = new RoleVo();
		List<MenuVo> menuList = MenuMapper.selectMenuWithPermission(roleId);
		roleVo.setMenuList(menuList);
		Role role = roleMapper.selectByPrimaryKey(roleId);
		if(role!=null){
			roleVo.setId(roleId);
			roleVo.setName(role.getName());
			roleVo.setRemark(role.getRemark());
		}
		return roleMapper.selectByPrimaryKey(roleId);
	}

	@Override
	public List<Role> selectAll() {
		RoleExample example = new RoleExample();
		example.setOrderByClause("id desc");
		return roleMapper.selectByExample(example);
	}
	@Transactional
	@Override
	public int deleteById(Integer roleId) {
		AdminRoleExample example = new AdminRoleExample();
		example.createCriteria().andRoleIdEqualTo(roleId);
		int count = adminRoleMapper.countByExample(example);
		if(count>0){
			return -1;
		}
		int flag = roleMapper.deleteByPrimaryKey(roleId);
		if(flag>0){
			roleMenuMapper.deleteByRoleId(roleId);
			rolePermissionMapper.deleteByRoleId(roleId);
		}
		return flag;
	}
	@Transactional
	@Override
	public int saveRoleInfo(Integer roleId, String name, String remark, String menuIds, String permissionIds) {
		RoleExample example = new RoleExample();
		example.createCriteria().andIdNotEqualTo(roleId).andNameEqualTo(name);
		int count = roleMapper.countByExample(example);
		if(count>0){
			return -1;
		}
		int flag =0;
		Role role = new Role();
		role.setName(name);
		role.setRemark(remark);
		if(roleId>0){
			role.setId(roleId);
			flag = roleMapper.updateByPrimaryKey(role);
			roleMenuMapper.deleteByRoleId(roleId);
			rolePermissionMapper.deleteByRoleId(roleId);
		}else{
			flag = roleMapper.insertSelective(role);
			roleId = role.getId();
		}
		if(!StringUtils.isBlank(menuIds)){
			String[] menuIdArray = menuIds.split(",");
			List<Integer> menuIdList = new ArrayList<>();
			for(String menuId:menuIdArray){
				menuIdList.add(Integer.valueOf(menuId));
			}
			if(menuIdList.size()>0){
				roleMenuMapper.batchInsert(menuIdList, roleId);
			}
		}
		if(!StringUtils.isBlank(permissionIds)){
			String[] permissionIdArray = permissionIds.split(",");
			List<Integer> list = new ArrayList<>();
			for(String permissionId:permissionIdArray){
				list.add(Integer.valueOf(permissionId));
			}
			if(list.size()>0){
				rolePermissionMapper.batchInsert(list, roleId);
			}
		}
		return flag;
	}
}
